﻿using System.Web.Mvc;
using Dexter.Logger.Contracts;
using Dexter.Search;
using Dexter.Services;
using Dexter.Web.Mvc.Controllers.Backoffice.Extensions;
using Dexter.Web.Mvc.Filters;
using Dexter.Web.Mvc.ViewModel.FrontEnd.Binders;
using Dexter.Web.Mvc.ViewModel.FrontEnd.ContactController;
using Dexter.Web.Mvc.ViewModel.ViewsData;
using Dexter.Web.Mvc.ViewsData;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.Controllers.FrontEnd {
	public class ContactController : PublicControllerBase {
		readonly IContactService contactService;

		public ContactController (ISearchService searchService , ICommentService commentService , IReferrerService referrerService , IConfigurationService configurationService , IWebUtils webUtils , ILogger logger , IUrlBuilder urlBuilder , IBlogRollService blogRollService , ICategoryService categoryService , IPostService postService , IThemeManager themeManager , ITagService tagService , IPageService pageService , IContactService contactService ) : base ( searchService , commentService , referrerService , configurationService , webUtils , logger , urlBuilder , blogRollService , categoryService , postService , themeManager , tagService , pageService) {
			this.contactService = contactService;
		}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		[DexterCallFilter ( true )]
		public ActionResult Index ( ) {
			ContactIndexViewData data = base.GetViewData ( new ContactIndexViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );
			data.Contact = ContactBinder.EmptyInstance;

			return View ( data );
		}

		[AcceptVerbs ( HttpVerbs.Post )]
		[ValidateAntiForgeryToken]
		[CaptchaValidateFilter]
		[DexterCallFilter ( false )]
		public ActionResult Index (ContactBinder contact ) {

			if (!ModelState.IsValid) {
				var data = base.GetViewData ( new ContactIndexViewData ( base.WebUtils, base.Configuration, base.UrlBuilder ) );
				data.Contact = contact;

				return View ( data );
			}

			contactService.SendMessage ( contact.ToInboxMessage ( ) );

			return View ( "Sent", base.GetViewData ( new PublicBaseViewData ( base.WebUtils, base.Configuration, base.UrlBuilder ) ) );
		}
	}
}
